<?php 

namespace Home\Controller;
use Think\Controller;

class DiscussController extends CommonController
{
	
	public function index(){
		$qid = I('get.qid');
		if(!$qid){
			exit;
		}
		M('question')->where(['qid'=>$qid])->setInc('view_num');
		$sql = "select t1.*,t2.nickname,t2.face from question t1 left join user t2 on t1.uid=t2.uid where t1.qid='{$qid}'";
		$msg = current(M()->query($sql));
		//var_dump($msg);exit;
		$this->assign('msg',$msg);
		$col = current(M('collect')->where(['uid'=>$_SESSION['uid'],'qid'=>$qid])->select());
		$this->assign('col',$col);

		$rows = M('question')->field('title,answer_num,qid')->order('answer_num desc')->limit(10)->select();
		$this->assign('rows',$rows);
		/*$sql_answer="select t1.*,t2.nickname,t2.face,t2.uid from answer t1 left join user t2 on t1.uid=t2.uid where t1.qid='{$qid}'";

		$ans = M()->query($sql_answer);*/

		$sql_answer="select t1.*,t2.nickname,t2.face,t2.uid from answer t1 left join user t2 on t1.uid=t2.uid where t1.qid='{$qid}'";
		$ans = M()->query($sql_answer); // 实例化User对象
		
		$count  = count($ans);// 查询满足要求的总记录数
		$Page = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page->show();// 分页显示输出
		$sql_page="select t1.*,t2.nickname,t2.face,t2.uid from answer t1 left join user t2 on t1.uid=t2.uid where t1.qid='{$qid}' limit ".$Page->firstRow.",".$Page->listRows;
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$pages = M()->query($sql_page);
		foreach ($pages as $k=>$v) {
			$res = M('zan')->where(['aid'=>$v['aid'],'uid'=>$_SESSION['uid']])->select();
			//var_dump($res);exit;
			if($res){
				$pages[$k]['is_zan']=1;
			}
		}
		//var_dump($pages);exit;
		$this->assign('pages',$pages);
		//var_dump($pages);exit;
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出

		$this->assign('title',$msg['title']);
		$this->display();
	}
	public function comment(){

		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Uploads/com_pic/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        //$this->error($upload->getError());
	        echo "失败";
	    }else{// 上传成功
	        $pic = __ROOT__.'/Uploads/com_pic/'.$info['file']['savepath'].$info['file']['savename'];

	        $arr=array(
	        	"code"=> 0,
	        	"msg"=> '成功',
	        	'data'=>array(
	        		'src'=>$pic,
	        		'title'=>'图片'
	        	)
	        );

	        echo json_encode($arr);
	    }

	 //    $data['uid'] = $_SESSION['uid'];
	 //    $data['qid'] = I('get.qid');

		// var_dump($data);
	}

	public function getcontent(){
		$data = I('post.');

		$check = D('user');

		$check->create($data,7);
		if(!$check){
			$this->ajaxReturn(["error"=>1,"info"=>'评论不能为空']);
		}
		//$get = $_SERVER;
		$data['uid'] = $_SESSION['uid'];
		//var_dump($get);exit;

		$data['create_time'] = time();

		$qid = $data['qid'];

		$auth = M('question')->where(['qid'=>$qid])->getField('uid');

		if($row=M('answer')->add($data)){
			M('question')->where(['qid'=>$qid])->setInc('answer_num');
			M('user')->where(['uid'=>$data['uid']])->setInc('answer_num');
			if($auth = $_SESSION['uid']){
				M('answer')->where(['aid'=>$row])->setField(['is_read'=>'1']);
			}else{
				M('user')->where(['uid'=>$auth])->setInc('msg');
			}
						
			$this->ajaxReturn(['error'=>0,'info'=>'评论成功']);
		};
	}

	public function zan(){
		$data = I('post.');
		$data['uid'] = $_SESSION['uid'];
		//var_dump($data);
		$zan = M('zan');
		$answer = M('answer');
		$res = current($zan->where(['uid'=>$data['uid'],'aid'=>$data['aid']])->select());
		//var_dump($res);
		if($res!=''){
			$zan->where(['id'=>$res['id']])->delete();
			$answer->where(['aid'=>$data['aid']])->setDec('zan_num');
			$this->ajaxReturn(['error'=>0,'info'=>'取消成功']);
		}else{
			$data['create_time']=time();
			$zan->add($data);
			$answer->where(['aid'=>$data['aid']])->setInc('zan_num');
			$this->ajaxReturn(['error'=>2,'info'=>'点赞成功']);
		}
	}

	public function collect(){
		$data = I('post.');
		//var_dump($data);
		$data['uid'] = $_SESSION['uid'];

		$collect = M('collect');
		$res = current($collect->where(['uid'=>$data['uid'],'qid'=>$data['qid']])->select());
		//var_dump($res);
		if($res!=''){
			$collect->where(['id'=>$res['id']])->delete();
			$this->ajaxReturn(['error'=>1,'info'=>'取消收藏成功']);
		}else{
			$data['create_time']=time();
			$collect->add($data);
			$this->ajaxReturn(['error'=>0,'info'=>'收藏成功']);
		}
	}

	public function adopt(){
		$data = I('post.');
		//var_dump($data);exit;
		$res = M('answer')->where(array('aid'=>$data['aid']))->setField(array('status'=>'1'));
		$qes = M('question')->where(array('qid'=>$data['qid']))->setField(array('status'=>'已采纳'));
		$this->ajaxReturn(['error'=>0,'info'=>'采纳成功']);
		//header("location:U('home/discuss/index')");
		//var_dump($qes);exit;
	}
}

 ?>